Skip to content

[Version 9.0] Feature support for record classes - #1458

Draft
BillWagner wants to merge 16 commits into
draft-v9from
v9-records
Draft

BillWagner wants to merge 16 commits into
draft-v9from
v9-records

Conversation

@BillWagner

@BillWagner BillWagner commented Nov 7, 2025 •

Copy link
Copy Markdown
Member

All commits from #983 have been squashed and added to this PR.

This PR / feature makes numerous changes to the grammar. We still need corresponding updates to the test and validation suite.

The following notes are carried over for additional work needed:

A. I put the new subclause "With expression" prior to "Arithmetic operators", which once the V8 features "Indices and Ranges" and "Pattern matching" have been merged, should immediately follow "Range operator" and "Switch expression." Make sure these are all in the correct place. 12.4.2 Operator precedence and associativity will also need to be revised accordingly.

B. New subclause §rec-class-prtmem Printing members mentions a method System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack(). It's quite possible that this is an implementation-specific detail, in which case, we'll need to find words to make it abstract, and possibly update the Portability annex accordingly.

@BillWagner BillWagner changed the title Feature support for records [Version 9.0] Feature support for records Nov 7, 2025
@RexJaeschke RexJaeschke added this to the C# 9.0 milestone Nov 8, 2025
Comment thread standard/expressions.md Resolved
Comment thread standard/expressions.md

An awaiter’s implementation of the interface methods `INotifyCompletion.OnCompleted` and `ICriticalNotifyCompletion.UnsafeOnCompleted` should cause the delegate `r` to be invoked at most once. Otherwise, the behavior of the enclosing async function is undefined.

## §with-expressions With expressions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add "with" as a new row to the table in the (earlier) section "Operator precedence and associativity." And as we have historically organized the sections in this clause in descending precedence order, we might need to move this new section up or down, accordingly.

Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md

The method performs the following tasks:

1. Calls the method `System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack()` if that method is present and the record class has printable members.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to expose this type in the spec?

@RexJaeschke RexJaeschke added the Review: pending Proposal is available for review label Nov 8, 2025
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
Comment thread standard/classes.md Resolved
@RexJaeschke RexJaeschke added the type: feature This issue describes a new feature label Nov 11, 2025
@Nigel-Ecma Nigel-Ecma self-assigned this Nov 19, 2025
@Nigel-Ecma Nigel-Ecma removed their assignment Apr 26, 2026
@RexJaeschke RexJaeschke changed the title [Version 9.0] Feature support for records [Version 9.0] Feature support for record classes May 9, 2026

@Nigel-Ecma Nigel-Ecma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a review looking at a high-level, the details semantics of records has not been reviewed.

Issue: The grammar changes break all the existing tests.

Often when adding new constructs to the language, records in this case, the existing tests all pass. In short this is down to the tests checking the shape of the tree, adding a new branch kind to a list of branch choices doesn’t change that (think adding a new statement kind to the list of statement kinds). However if where existing branches fit is changed then exisiting tests will fail. There is nothing wrong with this per se, but it should only be done if there is a good reason.

This proposal changes the location where classes fit into the overall grammar (by adding a branch node above them), there are a lot of classes in C# programs, and the result is all the tests fail. If there was a strong reason for this change then the expected parse for all tests could be updated (not hard, a little tedious).

However in this case the change is unnecessary:

  • class_declaration & class_body can be restored to their previous form;
  • record_class_body needs an optional ending semicolon, as it has in the source material;
  • type_declaration needs record_class_body added; and
  • non_record_class_declaration is consigned to the bin (yay)
class_declaration
    : attributes? class_modifier* 'partial'? 'class' identifier
        type_parameter_list? class_base? type_parameter_constraints_clause*
        class_body ';'?
    ;

class_body
    : '{' class_member_declaration* '}'
    ;

record_class_body
    : class_body ';'?
    | ';'
    ;

type_declaration
    : class_declaration
    | record_class_declaration
    | struct_declaration
    | interface_declaration
    | enum_declaration
    | delegate_declaration
    ;

All tests pass, including a new beta one for records (not yet released).

These changes do not fix all the concerns with the grammar for records, in particular there is unneeded sharing of rules – reduces the number of grammar rules but adds prose rules to split the overlaid rules up again (e.g. the source material has record_base and class_base, this proposal overlays them and adds prose rules), that’s not a win. (Sometimes overlaying makes sense, e.g. using class_body in record_body, no added prose rules required).

However these issues can be addressed in later reviews.

non-record class – no no no!

Sorry but this hurt my eyes and ears. Classes have been a central feature of C# of around a quarter of a century, changing them to be called non-record classes is a poor idea with no saving grace. Call them classes and remove any added non_record_class bits from rule names (note the grammar changes up consigned non_record_class_declaration to the bin).

I would also call records record, not record class, which wouldn’t prevent v10 adding record struct; it is just adding words for no real benefit.

The source material states:

Record types are reference types, similar to a class declaration.

Not that a record declaration is a class declaration, and uses rules such as record_declaration, record_base & record_body, in v10 it adds record_struct_X.

TL;DR

  • Restore the grammar rules listed at the top – don’t break things unnecessarily. And it “fixes” all the tests 🙂.
  • No “non-record class”, its a “class” with a quarter century of use.
  • Recommend dropping non_record_ prefix in grammar rule names (sometimes the source material gets it right 😉)

TTFN

@BillWagner BillWagner added the Review Requested: General direction This label requests a review for the broad direction only. PR is likely not ready to merge. label Nov 3, 2026
@BillWagner

Copy link
Copy Markdown
Member Author

Comments from initial review in committee meeting on November 4, 2026:

  • Let's strive to avoid the non_record_class_declaration grammar production. This would avoid more churn in the grammar. Can that be simplified?
  • Can we avoid the term "non-record class" when it means "the class we've always known".
  • Could we follow either the prior art for "non-nullable class type" or the prior art for ref structs.

Try this in separate branches to compare alternatives.

RexJaeschke and others added 16 commits November 12, 2026 12:14
Add support for record classes

Add files via upload

Add support for record classes

Add support for record classes

fix markup

fix markdown formatting

fix markdown formatting

fix link

another missed link

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Tweaks I made when comparing this PR with what I researched 2+ years ago.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: bce5e82a-89fd-4655-bc08-6d6ba97f0cc6
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
Exact canonical parser output; positive input has no parse or semantic errors. Records introduces the non_record_class_declaration node.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 21570b74-c6f5-4fc5-ab6d-7546918d6ddb
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
BillWagner added a commit that referenced this pull request Nov 12, 2026
Validated records source ce41281. Preserve the union of record, init, function-pointer and native-integer keywords with five-column alternatives.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
BillWagner added a commit that referenced this pull request Nov 12, 2026
Predecessor draft: cebb6e9; old alpha: 272e754. Current validated sources integrated in historical order: #1452, #1459, #1458, #1462, #1460, #1469, #1467, #1465, #1468, #1470, #1471, #1472. Already-merged native integers, static anonymous functions and lambda discard parameters come from the authoritative draft, not stale previews. Exact StandardAnchorTags ran last and identically twice with byte idempotence. All 604 actual example tests, source/compiler audits, pinned lint and converter pass. Downstream grammar-validator debt is recorded separately under Bill's current policy; no grammar remediation or waiver is claimed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 361b9a77-2b4c-45e6-969f-e6785c47f27e
@BillWagner BillWagner self-assigned this Nov 23, 2026
@BillWagner

Copy link
Copy Markdown
Member Author

During the December meeting, we should discuss these two alternative PRs (both target this branch):

#1880, create separate grammars for class types and record types.
#1881, use a single grammar for class types, treat record as a modifier on a class type.

@BillWagner BillWagner added meeting: priority Review before meeting. Merge, merge with issues, or reject at the next TC49-TC2 meeting and removed meeting: discuss This issue should be discussed at the next TC49-TG2 meeting labels Nov 23, 2026
@BillWagner

Copy link
Copy Markdown
Member Author

Adding some future version notes to inform our discussion on the options for specifying record types:

Type designators, primary constructors, and semicolon-only bodies

Type Declaration Primary constructor and base arguments Semicolon-only body
class class C { } class C(int x) : B(x) { } class C;
record class record class R { } record class R(int X) : RB(X) { } record class R;
struct struct S { } struct S(int x) { } struct S;
record struct record struct T { } record struct T(int X) { } record struct T;

Assume B is a class and RB a record class with suitable constructors.

  • C# 9: record introduces record classes, including primary constructors, base-constructor arguments, and semicolon-only bodies.
  • C# 10: Adds record struct and the explicit spelling record class, synonymous with record.
  • C# 12: Generalizes primary constructors and semicolon-only bodies to ordinary classes and structs. Base-constructor arguments require a primary constructor; structs have no class base-constructor list.
  • Record positional parameters synthesize corresponding properties unless matching members are declared. Ordinary primary-constructor parameters do not.

with expressions

For receiver with { ... }:

Receiver type Introduced Copying rule
record class C# 9 Invoke the receiver’s synthesized virtual clone method, which uses record copy construction. Preserves the runtime type.
record struct C# 10 Copy the receiver value. No clone method or copy constructor is invoked.
Ordinary struct C# 10 Same value-copy rule as record struct; no record-specific members are required.

In every case, evaluate the receiver once, obtain the copy, then process initializer assignments in lexical order. Assignments target accessible instance fields or properties and obey their normal assignment rules, including init and readonly restrictions. Ordinary classes are not eligible receivers.

Union declarations

C# 15 adds a distinct declaration form:

union Pet(Cat, Dog);

This declares a union represented by a struct. Its parenthesized list contains case types, not primary-constructor parameters. We will need to decide how deep we want to go into the lowering and its implication on custom unions.

Quick overview of custom unions: To provide language support for libraries that implement union like capabilities, interfaces and patterns can be used to make any type appear like a union for the purpose of pattern matching subsumption and exhaustiveness rules. I don't think that's relevant here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

meeting: priority Review before meeting. Merge, merge with issues, or reject at the next TC49-TC2 meeting Review: pending Proposal is available for review Review Requested: General direction This label requests a review for the broad direction only. PR is likely not ready to merge. type: feature This issue describes a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants